package com.jrx.anytxn.transaction.job.coupon.step;


import com.jrx.anytxn.customer.entity.CmCustomerRightInfo;
import com.jrx.anytxn.customer.entity.TlCouponDetail;
import com.jrx.anytxn.customer.service.ICustomerRightService;
import com.jrx.anytxn.customer.service.ITlCouponDetailService;
import com.jrx.anytxn.transaction.job.coupon.dto.CustomerRightsDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName CouponMonthlyWriter
 *
 * @author lujianyou
 * @version 1.0
 * @description 权益月活动 Writer
 * @date 2021/9/17 16:43
 **/
@Service
@Slf4j
public class CouponMonthlyWriter implements ItemWriter<CustomerRightsDTO> {

    @Autowired
    private ICustomerRightService customerRightService;

    @Autowired
    private ITlCouponDetailService tlCouponDetailService;

    @Override
    public void write(List<? extends CustomerRightsDTO> items) throws Exception {
        log.info("access into CouponMonthlyWriter->write()");
        if(items.isEmpty()){
            return;
        }
        List<TlCouponDetail> couponDetailList = new ArrayList<>(100);
        List<CmCustomerRightInfo> customerRightInfoList = new ArrayList<>(100);

        for (int i = 0; i < items.size(); i++) {
            CustomerRightsDTO item  = items.get(i);
            customerRightInfoList.add(item.getCmCustomerRightInfo());
            couponDetailList.add(item.getTlCouponDetail());
        }

        customerRightService.insertBatch(customerRightInfoList);
        tlCouponDetailService.insertBatch(couponDetailList);

    }

}
